Xceed DataGrid for WPF v7.2 Documentation
Sorting and displaying on different values

The following example demonstrates how to define foreign-key descriptions that sort on certain values but display these values differently, via a ForeignKeyConverter.  For instance, Employees will sort on last name then first name, but cells will display a photo, first name, and then last name.

XAML
Copy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">

 <Grid.Resources>

    <local:EmployeeForeignKeyConverter x:Key="employeeForeignKeyConverter" />

 

    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"

                                       Source="{Binding Source={x:Static Application.Current}, Path=Orders}">

            <xcdg:DataGridCollectionViewSource.ItemProperties>

               <xcdg:DataGridItemProperty Name="EmployeeID">

                  <xcdg:DataGridItemProperty.ForeignKeyDescription>

                     <xcdg:DataTableForeignKeyDescription ItemsSource="{Binding Source={x:Static Application.Current}, Path=Employees}"

                                                          ValuePath="EmployeeID"

                                                          ForeignKeyConverter="{StaticResource employeeForeignKeyConverter}" />

                  </xcdg:DataGridItemProperty.ForeignKeyDescription>

               </xcdg:DataGridItemProperty>

            </xcdg:DataGridCollectionViewSource.ItemProperties>

         </xcdg:DataGridCollectionViewSource>

 </Grid.Resources>      

 

 <xcdg:DataGridControl x:Name="OrdersGrid"

                       ItemsSource="{Binding Source={StaticResource cvs_orders}}"

                       AutoCreateForeignKeyConfigurations="True">

    <xcdg:DataGridControl.Columns>

            <xcdg:Column FieldName="EmployeeID"

                         Title="Employee"

                         Width="160">

               <xcdg:Column.CellContentTemplate>

                  <DataTemplate>

                     <DockPanel>

                        <Image Source="{Binding SmallPhoto}"

                               Height="20"

                               VerticalAlignment="Top"

                               Margin="1"

                               DockPanel.Dock="Left" />

                        <StackPanel Orientation="Horizontal">

                           <TextBlock Text="{Binding FirstName}" />

                           <TextBlock Text=" " />

                           <TextBlock Text="{Binding LastName}" />

                        </StackPanel>

                     </DockPanel>

                  </DataTemplate>

               </xcdg:Column.CellContentTemplate>

            </xcdg:Column>

    </xcdg:DataGridControl.Columns>

 </xcdg:DataGridControl>

</Grid> 

 The following code provides the implementation of the EmployeeForeignKeyConverter class.

VB.NET
Copy Code
Public Class EmployeeForeignKeyConverter

  Inherits DataTableForeignKeyConverter

  'Only the GetValueFormKey method overload with the DataGridForeignKeyDescription parameter needs to be overridden,

  'for the base class DataTableForeignKeyConverter already provides an implementation of the other two overridable methods.

  Public Overrides Function GetValueFromKey(ByVal key As Object, ByVal description As DataGridForeignKeyDescription) As Object

    If key Is Nothing Then

      Return Nothing

    End If



    Dim dataView As DataView = TryCast(description.ItemsSource, DataView)

    If dataView IsNot Nothing Then

      dataView.Sort = description.ValuePath



      Dim index As Integer = dataView.Find(key)

      Dim dataRow As DataRowView = dataView(index)



      'Return a value built in this order, so sorting is done on last name, then first name.

      Return dataRow("LastName") & ", " & dataRow("FirstName")

    End If



    Return key

  End Function

End Class 
C#
Copy Code
public class EmployeeForeignKeyConverter : DataTableForeignKeyConverter

  {

    //Only the GetValueFormKey method overload with the DataGridForeignKeyDescription parameter needs to be overridden,

    //for the base class DataTableForeignKeyConverter already provides an implementation of the other two overridable methods.

    public override object GetValueFromKey( object key, DataGridForeignKeyDescription description )

    {

      if( key == null )

        return null;



      var dataView = description.ItemsSource as DataView;

      if( dataView != null )

      {

        dataView.Sort = description.ValuePath;



        var index = dataView.Find( key );

        var dataRow = dataView[ index ];



        //Return a value built in this order, so sorting is done on last name, then first name.

        return dataRow[ "LastName" ] + ", " + dataRow[ "FirstName" ];

      }



      return key;

    }

  }